<template src="./template.html"></template>

<style src="./style.css" scoped></style>

<script>
    import incomeBg from '../assets/images/income-bg.png';
    import freezeMoneyIcon from 'assets/images/help.png';
    import listMoreIcon from '../assets/images/more-icon.png';
    import api from "api/api";
    import modal from "utils/modal";
    import navigator from 'utils/navigator';
    import route from 'constants/route';
    import constant from 'constants/constant';
    const moneyType = 1; //余额明细

    export default{
        data() {
            return {
                incomeBg: incomeBg,
                freezeMoneyIcon: freezeMoneyIcon,
                listMoreIcon: listMoreIcon,
                incomeMoney: 0, //余额
                freezeMoney: 0, //冻结余额
                incomeList: [],
                start: 0,
                limit: 10,
                refEl: 'cell_0',//n起始值为0
                showGoTop: false,//默认置顶按钮为隐藏
                loading: false, //正在加载
                listLoaded: false, //默认的页面没有加载
                listCompleted: false //分页完成
            }
        },
        created() {
            let self = this;
            let _params = {
                start: self.start,
                limit: self.limit,
                type: moneyType
            };

            self.sendHttp(_params,res=>{
                self.listLoaded = true;
                self.incomeMoney = res.data.userMoney || 0; //余额
                self.freezeMoney = res.data.userFrozenMoney || 0; //冻结余额
                self.incomeList = res.data.moneyFinance || [];


                 //总数据量小于limit
                 if(self.incomeList.length < self.limit) {
                    self.listCompleted=true;
                 }
            });
        },
        methods: {
            sendHttp(params,succCallback,failCallback){
                api.sendHttp("getMyMoneyApi",params).then(res=>{
                    if(typeof succCallback == "function") succCallback(res);
                },
                res=>{
                    modal.toast(res.msg);
                    if(typeof failCallback == "function") failCallback(res);
                });
            },
            fetchMore(){
                this.$refs['list'].resetLoadmore(); //下一次滚动时强制触发loadmore

                let self = this;
                if(self.listCompleted || self.loading) {
                    return false;
                }

                self.loading = true;
                let temp = self.start + self.limit;
                self.start = temp;

                let _params = {
                    start: self.start,
                    limit: self.limit,
                    type: moneyType
                };

                self.sendHttp(_params,res=>{
                    self.loading = false;
                    let data = res.data || {};
                    let _incomeList = data.moneyFinance || [];

                    if(_incomeList.length < self.limit) self.listCompleted = true;
                    self.incomeList = self.incomeList.concat(_incomeList);
                });
            },
            goToFreeze(){
                if(this.freezeMoney <= 0) return false;
                let options = {
                    classType: constant.Pop_Class_Type,
                    url: route.my_money_freeze_index,
                    title: "冻结余额"
                };
                navigator.push(options);
            },
            goToDetail(income){
                let options = {
                    classType: constant.Pop_Class_Type,
                    url: route.my_money_income_detail,
                    title: "收支明细详情",
                    params: {
                        income: income || {}
                    }
                };
                navigator.push(options);
            },
            onScroll(e) {
                let contentOffset = e.contentOffset;
                let offsetY = contentOffset.y;
                if(offsetY <= -2000){
                    if(!this.showGoTop) this.showGoTop = true;
                } else if(offsetY > -2000) {
                    if(this.showGoTop) this.showGoTop = false;
                }
            }
        }
    }
</script>
